<?php
/**
 * 轻账本模块
 *
 */

class Api_Account extends PhalApi_Api {

    public function getRules() {
        return array(
            'getLastAccount' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
            ),
            'getAccountList' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'pageNum'   => array('name' => 'pageNum', 'type' => 'int', 'default' => 1, 'desc' => '页数'),
                'pageSize'   => array('name' => 'pageSize', 'type' => 'int', 'default'=>10, 'desc' => '每页数量'),
            ),
            'addAccount' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'note'   => array('name' => 'note', 'type' => 'string', 'desc' => '心情'),
            ),
            'addAccountDetail' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'accountId' => array('name' => 'accountId', 'type' => 'int', 'require' => true, 'desc' => '所属账本ID'),
                'cateId' => array('name' => 'cateId', 'type' => 'int', 'require' => true, 'desc' => '类别ID'),
                'amount'   => array('name' => 'amount', 'type' => 'float', 'require' => true, 'desc' => '金额'),
            ),
            'saveAccount' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'accountJson' => array('name' => 'accountJson', 'type' => 'string', 'require' => true, 'desc' => '账本JSON数据'),
                'detailJson' => array('name' => 'detailJson', 'type' => 'string', 'require' => true, 'desc' => '账本资产JSON数据，解析为数组'),
                'note'   => array('name' => 'note', 'type' => 'string', 'default' => "", 'desc' => '心情'),
            ),
            'deleteAccount' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'accountId' => array('name' => 'accountId', 'type' => 'int', 'require' => true, 'desc' => '归档账本ID'),
            ),
        );
    }

    /**
     * 获取最新账本信息
     * @desc 获取最新账本信息
     * @return int code 操作码，0表示成功， -1表示获取失败 -2 表示token 失效
     * @return string msg 返回结果信息
     * @return object info 最新账本信息
     */
    public function getLastAccount() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $modelAccount = new Model_Account();
        $rowAccount = $modelAccount->getLastAccount($this->userId);
        if($rowAccount){
            $rs['msg'] = "获取最新账本成功";
            $rs['info'] = $rowAccount;
        } else {
            $rs['code'] = -1;
            $rs['msg'] = "获取最新账本失败";
        }
        return $rs;
    }

    /**
     * 添加账本
     * @desc 添加一个主账本
     * @return int code 操作码，0表示成功， -1表示获取失败 -2 表示token 失效
     * @return string msg 返回结果信息
     * @return object into 返回结果
     * @return info.accountId 返回所添加账本ID
     */
    public function addAccount() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $domain = new Domain_Account();
        $accountId = $domain->addAccount($this->userId,$this->note);
        if($accountId > 0){
            $rs['msg'] = '添加成功';
            $rs['info']['accountId'] = $accountId;
        } else {
            $rs['code'] = -1;
            $rs['msg'] = '添加失败';
        }
        return $rs;
    }

    /**
     * 添加账本详情
     * @desc 添加一个账单详情
     * @return int code 操作码，0表示成功， -1表示获取失败 -2 表示token 失效
     * @return string msg 返回结果信息
     * @return object into 返回结果
     */
    public function addAccountDetail() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $domain = new Domain_Account();
        $model =  new Model_Account();
        // NOTE: 判断当前用户是否持有当前账本
        if(!$model->checkAccountValid($this->userId,$this->accountId)){
            $rs['code'] = -1;
            $rs['msg'] = "当前用户没有持有当前账本";
            return $rs;
        }

        if($domain->addAccountDetail($this->accountId,$this->cateId,$this->amount)){
            $rs['msg'] = "添加账本详情成功";
        } else {
            $rs['code'] = -1;
            $rs['msg'] = "添加账本详情失败";
        }
        return $rs;
    }

    /**
     * 获取账本列表
     * @desc 获取账本列表
     * @return int code 操作码，0表示成功， -1 获取失败 -2 表示token 失效
     * @return string msg 返回结果信息
     * @return object into 返回结果
     * @return array info.accountList 账单列表
     * @return float info.accountList[0].amount 账本金额
     * @return string info.accountList[0].date 账本日期
     * @return string info.accountList[0].note 心情
     * @return array info.accountList[0].detail 账本详情
     */
    public function getAccountList() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $model =  new Model_Account();
        $rowsAccount = $model->getAccountList($this->userId,$this->pageNum,$this->pageSize);
        $rs['info']['accountList'] = $rowsAccount;
        $rs['msg'] = "获取成功";
        return $rs;
    }

    /**
     * saveAccount 保存账本记录
     * @desc 保存账本，将本地的账本归档服务器中
     * @return int code 操作码，0表示成功， -1 操作失败 -2 表示token 失效
     * @return string msg 返回结果信息
     */
    public function saveAccount() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $model = new Model_Account();
        if($model->saveAccount($this->userId,$this->accountJson, $this->detailJson, $this->note)) {
            $rs['msg'] = '账本数据保存成功';
        } else {
            $rs['code'] = -1;
            $rs['msg'] = '账本数据保存失败，查看当前APP是否是最新版';
        }
        return $rs;
    }

    /**
     * [deleteAccount 删除已归档账本]
     * @desc 将已归档的账本数据删除
     * @return int code 操作码，0表示成功， -1 操作失败 -2 表示token 失效
     * @return string msg 返回结果信息
     */
    public function deleteAccount() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        $model = new Model_Account();
        if ($model->deleteAccount($this->userId, $this->accountId)) {
            $rs['msg'] = '删除归档账本成功！';
        } else {
            $rs['code'] = -1;
            $rs['msg'] = '删除归档账本失败';
        }
        return $rs;
    }

}
